package com.bob.common.bean;

import cn.hutool.json.JSONObject;
import com.bob.common.utils.PomUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class PomEntity extends PomBaseEntity {

    private PomEntity parent ;
    private String packaging ;
    // F:\dev\cczyz\cczyz\cczyz_app
    private String absolutePath ;

    private  String finalName;
    private List<String> modules = new ArrayList<>();

    // dependency
    private List<PomBaseEntity> dependencies = new ArrayList<>();
    public String getPackaging() {
        return packaging;
    }
    public void setPackaging(String packaging) {
        this.packaging = packaging;
    }
    public String getAbsolutePath() {
        return absolutePath;
    }
    public void setAbsolutePath(String absolutePath) {
        this.absolutePath = absolutePath;
    }

    public List<String> getModules() {
        return modules;
    }

    public void setModules(List<String> modules) {
        this.modules = modules;
    }

    public PomEntity getParent() {
        return parent;
    }

    public void setParent(PomEntity parent) {
        this.parent = parent;
    }

    public String getFinalName() {
        return finalName;
    }

    public void setFinalName(String finalName) {
        this.finalName = finalName;
    }

    public List<PomBaseEntity> getDependencies() {
        return dependencies;
    }

    public PomEntity getDependence(String artifactId){
        Optional<PomBaseEntity> baseEntity = getDependencies().stream().filter(pomBaseEntity -> pomBaseEntity.getArtifactId().equals(artifactId)).findAny();
        if(baseEntity.isPresent() ){
            PomEntity pomEntity = (PomEntity)baseEntity.get();
                pomEntity.setFinalName(PomUtil.finalName(pomEntity.getArtifactId(),pomEntity.getVersion()));
                pomEntity.setPackaging(Constant.FILETYPE_JAR);
            return  pomEntity;
        }
        return  null;

    }

    @Override
    public String toString() {
        return new JSONObject(this).toJSONString(4);
    }
}
